package org.dairy.farms.dao.hibernate;

import org.dairy.farms.dao.FeedDAO;
import org.dairy.farms.dao.generic.AbstractDAOImpl;
import org.dairy.farms.model.Feed;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jul 12, 2011
 * Time: 4:38:00 PM
 */
@Repository("feedDao" )
public class FeedDAOImpl extends AbstractDAOImpl<Feed,Long> implements FeedDAO {
    @Override
    public List<Feed> getFeedDetails(Long livestockId, int page, int start, int limit) {
        Criteria criteria = basePersistence.getSession().createCriteria(Feed.class);
        criteria.createCriteria("livestock").add(Restrictions.eq("id",livestockId)).setFirstResult(start).setMaxResults(limit);
        criteria.createAlias("feedType","feedType").createAlias("feedExpense","feedExpense");
        criteria.addOrder(Property.forName("id").asc());
        return criteria.list();
    }

    @Override
    public long getFeedDetailsTotalCount(Long livestockId) {
        Criteria criteria = basePersistence.getSession().createCriteria(Feed.class);
        criteria.createCriteria("livestock").add(Restrictions.eq("id",livestockId));
        criteria.setProjection(Projections.rowCount());
        return (Long)criteria.uniqueResult();
    }
}
